package com.xci.xajtfb.bus.service;

import cn.hutool.core.util.StrUtil;
import com.github.lvyanyang.core.BoolResult;
import com.github.lvyanyang.core.PageList;
import com.github.lvyanyang.core.XCI;
import com.xci.xajtfb.bus._core.BusBaseService;
import com.xci.xajtfb.bus.dao.WxComplaintDao;
import com.xci.xajtfb.bus.dao.WxSerialDao;
import com.xci.xajtfb.bus.entity.BaseFile;
import com.xci.xajtfb.bus.entity.WxComplaint;
import com.xci.xajtfb.bus.entity.WxSerial;
import com.xci.xajtfb.bus.filter.WxComplaintFilter;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.internal.util.StringHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 出租投诉信息表服务
 * @author 张谦 765870682@qq.com
 * @since 2021-03-02 9:40:00
 */
@Slf4j
@Service
public class WxComplaintService extends BusBaseService {

    /**
     * 出租投诉信息表数据层对象
     */
    @Resource
    private WxComplaintDao wxComplaintDao;

    /**
     * 系统文件服务
     */
    @Resource
    private BaseFileService baseFileService;

    /**
     * 序列表数据层
     */
    @Resource
    private WxSerialDao wxSerialDao;

    /**
     * 新增前，根据车号 查询出租车公司
     * @param carNumber  车牌号码
     */
    private BoolResult checkEntity(String carNumber) {
        //查询出租车公司

        return BoolResult.success();
    }

    /**
     * 新建出租投诉信息表
     * @param entity 数据对象
     * @return 执行成功返回success, 否则返回fail
     */
    @Transactional(rollbackFor = Exception.class)
    public BoolResult insert(WxComplaint entity) {
        //主键
        entity.setComplaintId(XCI.nextIdStr());
        //投诉日期
        entity.setDate(new Date());
        //状态
        entity.setState(0);
        //
        entity.setDeleted(1);
        BoolResult checkResult = checkEntity(entity.getCarNumber());
        if(checkResult.isFail()) return checkResult;

        //生成序号
        entity.setSerial(GetSerial());
        wxComplaintDao.insert(entity);

        if(!StringHelper.isNullOrEmptyString(entity.getUploadImgs())){
            //文件上传
            String[] UploadImga = entity.getUploadImgs().split(",");
            for(String UploadImg : UploadImga) {
                BaseFile baseFile = new BaseFile();
                baseFile.setName("出租投诉信息-"+entity.getCarNumber());
                baseFile.setVirtualUrl(UploadImg);
                baseFile.setContentType("image/jpeg");
                baseFile.setSize(0L);
                baseFile.setResId(entity.getComplaintId());
                BoolResult boolResult = baseFileService.insert(baseFile);
            }
        }
        return BoolResult.success();
    }

    public String GetSerial()
    {
        SimpleDateFormat formatter= new SimpleDateFormat("yyyyMMdd");
        Date newDate = new Date(System.currentTimeMillis());
        var date =formatter.format(newDate) ;
        WxSerial oldWxSerial = wxSerialDao.selectByDate(date);
        WxSerial wxSerial =new WxSerial();
        wxSerial.setDate(date);
        if (oldWxSerial!=null )
        {
            wxSerial.setSerial(oldWxSerial.getSerial()+1);
            wxSerialDao.update(wxSerial);
        }
        else
        {
            wxSerial.setSerial(1);
            wxSerialDao.insert(wxSerial);
        }
        var seqNumber = StrUtil.fillBefore(wxSerial.getSerial().toString(), '0', 5);
        String Serial = "TS"+date+seqNumber;//TS+20210302+00002
        return Serial;
    }

    /**
     * 根据主键查询单个出租投诉信息表
     * @param id 主键
     * @return 返回单个数据对象
     */
    public WxComplaint selectById(@NotBlank(message = "请指定出租投诉信息表主键") String id) {
        return wxComplaintDao.selectById(id);
    }

    /**
     * 查询出租投诉信息表分页列表
     * @param filter 过滤条件
     * @return 返回符合条件的分页数据集合
     */
    public PageList<WxComplaint> selectPageList(WxComplaintFilter filter) {
        return PageList.of(wxComplaintDao.selectPageList(filter));
    }
}
